package com.ruoyi.womp.mqhandler;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Random;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.womp.config.RabbitMQConfig;
import com.ruoyi.womp.tools.DateUtil;

@Component("realTimeDataPushTest2")
public class RealTimeDataPushTestTask2 {

	final static String TASK_NAME = "测试SGS001汽车衡";

	@Autowired
	RabbitTemplate rabbitTemplate;
	@Autowired
	RedisTemplate<String, String> redisTemplate;

	public void execute() {
		try {
			String waCode = "SGSC001";

			int numXW = 4;
			int numLD = 8;
			int numCZ = 10;
			int numZL = 1;
			int numAC = 1;
			int numWD = 1;

			Random random = new Random();

			JSONObject dataJsonObject = new JSONObject();
			dataJsonObject.put("type", 1);

			JSONObject messageJsonObject = new JSONObject();
			messageJsonObject.put("waCode", waCode);
			messageJsonObject.put("time", DateUtil.sysDateTime());

			JSONArray dataJsonArray = new JSONArray();
			// 垂直度
			for (int i = 1; i <= numXW; i++) {
				List<Double> list = new ArrayList<Double>();
				list.add(5.5);
				list.add(6.1);
				list.add(5.2);
				list.add(5.6);
				
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "XW");
				jsonObject.put("orderNum", i);
				Double value = list.get(i - 1) + Math.random() * 0.01;
				DecimalFormat df2  = new DecimalFormat("###.00");
				jsonObject.put("value", df2.format(value));

				dataJsonArray.add(jsonObject);
			}

			// 分零点
			for (int i = 1; i <= numLD; i++) {
				List<Integer> list = new ArrayList<Integer>();
				list.add(1235);
				list.add(4523);
				list.add(45);
				list.add(75);
				list.add(34);
				list.add(458);
				list.add(353);
				list.add(234);

				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "LD");
				jsonObject.put("orderNum", i);
				jsonObject.put("value", list.get(i - 1));

				dataJsonArray.add(jsonObject);
			}
			
			// 垂直
			for (int i = 1; i <= numCZ; i++) {
				List<Double> list = new ArrayList<Double>();
				list.add(0.01);
				list.add(0.00);
				list.add(0.02);
				list.add(0.00);
				list.add(0.00);
				list.add(0.01);
				list.add(0.00);
				list.add(0.11);
				list.add(0.00);
				list.add(0.11);

				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "CZ");
				jsonObject.put("orderNum", i);
				Double value = Math.random() * (0.02 - 0.01) + 0.01;
				jsonObject.put("value", list.get(i - 1));

				dataJsonArray.add(jsonObject);
			}

			String weightData = redisTemplate.boundValueOps("weight_data:" + waCode).get();
			String now = DateUtil.formatDateTime(Calendar.getInstance().getTime());
			if (null == weightData || JSONObject.parseObject(weightData).getString("startTime").equals(now)) {

				JSONObject weightJsonObject = new JSONObject();
				weightJsonObject.put("startTime", now);
				if (null != weightData) {
					weightJsonObject = JSONObject.parseObject(weightData);
				}

				int emptyV = 19500;
				int fullV = 78300;

				int type = random.nextInt(2);

				int heightV = emptyV;
				if (type == 1) {
					heightV = fullV;
				}
				int maxWeight = random.nextInt(15000) + heightV;

				int time = random.nextInt(10) + 20;
				int currTime = 1;

				weightJsonObject.put("maxWeight", maxWeight);
				weightJsonObject.put("time", time);

			}
			// 总零点（重量）
			for (int i = 1; i <= numZL; i++) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "ZL");
				jsonObject.put("orderNum", i);
				jsonObject.put("value", 0);

				dataJsonArray.add(jsonObject);
			}
			// redisTemplate.boundValueOps("realtime_data:" +
			// waCode).set(newJsonObject.toJSONString(), 1,
			// TimeUnit.MINUTES);

			// 电压
			for (int i = 1; i <= numAC; i++) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "AC");
				jsonObject.put("orderNum", i);
				jsonObject.put("value", 220);

				dataJsonArray.add(jsonObject);
			}

			// 室外温度
			for (int i = 1; i <= numWD; i++) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put("code", "WD");
				jsonObject.put("orderNum", i);
				jsonObject.put("value", random.nextInt(1) + 26);

				dataJsonArray.add(jsonObject);
			}

			messageJsonObject.put("data", dataJsonArray);
			dataJsonObject.put("message", messageJsonObject);

			//System.out.println(dataJsonObject.toJSONString());
			rabbitTemplate.convertAndSend(RabbitMQConfig.MQ_EXCHANGE_FANOUT_REALTIME_DATA,
					RabbitMQConfig.MQ_ROUTINGKEY_REALTIME_DATA, dataJsonObject.toJSONString());

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}